package com.gangquan360.smartadmin.module.customer.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.gangquan360.smartadmin.common.domain.PageResultDTO;
import com.gangquan360.smartadmin.common.domain.ResponseDTO;
import com.gangquan360.smartadmin.module.customer.dao.CustomerDao;
import com.gangquan360.smartadmin.module.customer.dao.CustomerInvoiceInfoDao;
import com.gangquan360.smartadmin.module.customer.domain.dto.CustomerDTO;
import com.gangquan360.smartadmin.module.customer.domain.dto.CustomerQueryDTO;
import com.gangquan360.smartadmin.module.customer.domain.entity.CustomerEntity;
import com.gangquan360.smartadmin.module.customer.domain.entity.CustomerInvoiceInfoEntity;
import com.gangquan360.smartadmin.util.SmartBeanUtil;
import com.gangquan360.smartadmin.util.SmartPaginationUtil;

import lombok.extern.slf4j.Slf4j;

/**
 * [ 客户表 ]
 *
 * @author wiggin
 * @version 1.0
 * @company wiggin
 * @copyright (c) 2019 wigginInc. All rights reserved.
 * @date 2020-03-08 21:09:45
 * @since JDK1.8
 */
@Slf4j
@Service
public class CustomerService {

    @Autowired
    private CustomerDao customerDao;
    
    @Autowired
    private CustomerInvoiceInfoDao customerInvoiceInfoDao;


    /**
     * @author wiggin
     * @description 分页查询
     * @date 2020-03-08 21:09:45
     */
    public ResponseDTO<PageResultDTO<CustomerDTO>> queryByPage(CustomerQueryDTO queryDTO) {
        Page<CustomerDTO> page = SmartPaginationUtil.convert2PageQueryInfo(queryDTO);
        List<CustomerEntity> entities = customerDao.queryByPage(page, queryDTO);
        List<CustomerDTO> dtoList = SmartBeanUtil.copyList(entities, CustomerDTO.class);
        page.setRecords(dtoList);
        PageResultDTO<CustomerDTO> pageResultDTO = SmartPaginationUtil.convert2PageResultDTO(page);
        return ResponseDTO.succData(pageResultDTO);
    }
    
    /**
     * 
     * @return
     */
    public ResponseDTO<List<CustomerDTO>> queryList() {
    	Wrapper<CustomerEntity> wrapper = new EntityWrapper<>();
		List<CustomerEntity> selectList = customerDao.selectList(wrapper);
		List<CustomerDTO> copyList = SmartBeanUtil.copyList(selectList, CustomerDTO.class);
    	return ResponseDTO.succData(copyList);
    }
    

    /**
     * @author wiggin
     * @description 添加
     * @date 2020-03-08 21:09:45
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<String> add(CustomerDTO addDTO) {
        CustomerEntity entity = SmartBeanUtil.copy(addDTO, CustomerEntity.class);
        customerDao.insert(entity);
        CustomerInvoiceInfoEntity invoiceInfoEntity = SmartBeanUtil.copy(addDTO, CustomerInvoiceInfoEntity.class);
        invoiceInfoEntity.setCustomerId(entity.getId());
        customerInvoiceInfoDao.insert(invoiceInfoEntity);
        return ResponseDTO.succ();
    }


    /**
     * @author wiggin
     * @description 编辑
     * @date 2020-03-08 21:09:45
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<String> update(CustomerDTO updateDTO) {
        CustomerEntity entity = SmartBeanUtil.copy(updateDTO, CustomerEntity.class);
        customerDao.updateById(entity);
        CustomerInvoiceInfoEntity queryEntity = new CustomerInvoiceInfoEntity();
        queryEntity.setCustomerId(entity.getId());
        CustomerInvoiceInfoEntity selectOne = customerInvoiceInfoDao.selectOne(queryEntity);
        CustomerInvoiceInfoEntity invoiceInfoEntity = SmartBeanUtil.copy(updateDTO, CustomerInvoiceInfoEntity.class);
        if (selectOne != null) {
        	customerInvoiceInfoDao.updateByCustomerId(entity.getId(), invoiceInfoEntity);
        } else {
        	invoiceInfoEntity.setCustomerId(entity.getId());
        	customerInvoiceInfoDao.insert(invoiceInfoEntity);
        }
        return ResponseDTO.succ();
    }


    /**
     * @author wiggin
     * @description 删除
     * @date 2020-03-08 21:09:45
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<String> delete(Long id) {
        customerDao.deleteById(id);
        customerInvoiceInfoDao.deleteByCustomerId(id);
        return ResponseDTO.succ();
    }

    /**
     * @author wiggin
     * @description  根据ID查询
     * @date 2020-03-08 21:09:45
    */
    public ResponseDTO<CustomerDTO> detail(Long id){
    	CustomerDTO dto = customerDao.getAllDetail(id);
    	log.info("dto => {}", dto);
        return ResponseDTO.succData(dto);
    }
}
